<?php
namespace Article\Controller;
use Think\Controller;
class IndexController extends Controller {
    //TP构造函数
    public function _initialize(){
        //自动加载的类
        new \Org\Admin_autoload();
    }
    //文章首页/列表页
    public function index(){
        $data['title']='文章管理';
        $data['active']='article_index';
        $this->assign('data',$data);
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $arr['page']=$page;
        $arr['perpage']=$perpage;
        $Model=D("Article");
        $article_list=$Model->get_article_list($arr);
        $article_total=$Model->get_article_total();
        $page_total=ceil($article_total/$perpage);
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['page_total']=$page_total;
        //dump($pager);exit;
        $this->assign("article_list", $article_list);
        $this->assign("perpage", $perpage);
        $this->assign("pager",$pager);
        //$this->assign("page", $page);
        $this->assign("article_total", $article_total);
        $this->assign("page_total", $page_total);
        $this->display();
    }

    //编辑文章
    public function article_edit()
    {
        $data['title']='编辑文章';
        $data['active']='article_index';
        $this->assign('data',$data);
        $article_id=I('path.2');
        $this->assign("article_id", $article_id);
        $Model=D("Article");
        $article_info=$Model->get_article_info($article_id);
        //dump($blog_info);exit;
        $this->assign("article_info", $article_info);
        $CategoryModel=D("ArticleCategory");
        $category_list=$CategoryModel->get_category_list();
        //dump($category_list);exit;
        $this->assign("category_list", $category_list);
        $this->display();
    }

    //编辑文章
    public function article_edit_post()
    {
        //提交检测
        if($_POST['article_title']==''){
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
        $arr=I('post.');
        //当类型为演员时，才检查是否上传图片
        if($_POST['article_type']==2){
            if(empty($_FILES['article_thumb']['tmp_name'])){
                //echo "<script>alert('您没有选择文件');history.back(-1);</script>";exit;
            }else{
                //上传图片
                $root_path = ROOT_PATH . 'www/home/data/upload/images';
                if (!is_dir($root_path)) {
                    mkdir($root_path);
                    chmod($root_path, 0777);
                }
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 15000000;// 设置附件上传大小 15M
                $upload->exts = array('jpeg', 'jpg','gif','png');// 设置附件上传类型
                $upload->rootPath = $root_path . '/'; // 设置附件上传根目录
                $upload->savePath = ''; // 设置附件上传（子）目录
                $upload->subName = '';
                $upload->saveName = 'uniqid';
                $info = $upload->upload();
                if (!$info) {
                    echo "<script>alert('".$upload->getError()."');history.back(-1);</script>";exit;
                }
                //处理缩略图
                $image_path=$root_path.'/'.$info['article_thumb']['savename'];
                $small_image_path=substr($image_path,0,-4).'_small.'.$info['article_thumb']['ext'];
                $small_image_url='/data/upload/images/'.substr($info['article_thumb']['savename'],0,-4).'_small.'.$info['article_thumb']['ext'];
                $large_image_path=substr($image_path,0,-4).'_large.'.$info['article_thumb']['ext'];
                $large_image_url='/data/upload/images/'.substr($info['article_thumb']['savename'],0,-4).'_large.'.$info['article_thumb']['ext'];
                $image = new \Think\Image();
                //切小图
                $image->open($image_path);
                //设置缩略图最大宽度、高度
                $small_image_max_width=40;
                $small_image_max_height=40;
                $image->thumb($small_image_max_width, $small_image_max_height)->save($small_image_path);
                //切大图
                $image->open($image_path);
                $large_image_max_width=375;
                $large_image_max_height=667;
                $image->thumb($large_image_max_width, $large_image_max_height)->save($large_image_path);
                //删除原图
                unlink($image_path);
                $arr['article_thumb']=$small_image_url;
                $arr['article_image']=$large_image_url;
            }
        }
        //一些数据检测
        //$arr['article_content']=I('post.article_content','','');
        if(I('post.article_sort')==''){
            $arr['article_sort']=999;
        }
        $Model=D("Article");
        if(!$Model->edit_article_post($arr)){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }else{
            //echo "<script>alert('提交成功');window.location.href='/article';</script>";exit;
            echo "<script>window.history.go(-2);window.location.reload()</script>";exit;
        }
    }

    //添加文章
    public function article_add()
    {
        $data['title']='添加文章';
        $data['active']='article_index';
        $this->assign('data',$data);
        $CategoryModel=D("ArticleCategory");
        $category_list=$CategoryModel->get_category_list();
        $this->assign("category_list", $category_list);
        $this->display();
    }

    //添加文章
    public function article_add_post()
    {
        //提交检测
        if($_POST['article_title']==''){
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
        $arr=I('post.');
        //当类型为演员时，才检查是否上传图片
        if($_POST['article_type']==2){
            if(empty($_FILES['article_thumb']['tmp_name'])){
                //echo "<script>alert('您没有选择文件');history.back(-1);</script>";exit;
            }else{
                //上传图片
                $root_path = ROOT_PATH . 'www/home/data/upload/images';
                if (!is_dir($root_path)) {
                    mkdir($root_path);
                    chmod($root_path, 0777);
                }
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 10048000;// 设置附件上传大小 10M
                $upload->exts = array('jpeg', 'jpg','gif','png');// 设置附件上传类型
                $upload->rootPath = $root_path . '/'; // 设置附件上传根目录
                $upload->savePath = ''; // 设置附件上传（子）目录
                $upload->subName = '';
                $upload->saveName = 'uniqid';
                $info = $upload->upload();
                if (!$info) {
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                //处理缩略图
                $image_path=$root_path.'/'.$info['article_thumb']['savename'];
                $small_image_path=substr($image_path,0,-4).'_small.'.$info['article_thumb']['ext'];
                $small_image_url='/data/upload/images/'.substr($info['article_thumb']['savename'],0,-4).'_small.'.$info['article_thumb']['ext'];
                $large_image_path=substr($image_path,0,-4).'_large.'.$info['article_thumb']['ext'];
                $large_image_url='/data/upload/images/'.substr($info['article_thumb']['savename'],0,-4).'_large.'.$info['article_thumb']['ext'];
                $image = new \Think\Image();
                //切小图
                $image->open($image_path);
                //设置缩略图最大宽度、高度
                $small_image_max_width=40;
                $small_image_max_height=40;
                $image->thumb($small_image_max_width, $small_image_max_height)->save($small_image_path);
                //切大图 大图暂定为苹果6标准
                $image->open($image_path);
                $large_image_max_width=375;
                $large_image_max_height=667;
                $image->thumb($large_image_max_width, $large_image_max_height)->save($large_image_path);
                //删除原图
                unlink($image_path);
                $arr['article_thumb']=$small_image_url;
                $arr['article_image']=$large_image_url;
            }
        }
        //一些数据检测
        //$arr['article_content']=I('post.article_content','','');
        if(I('post.article_sort')==''){
            $arr['article_sort']=999;
        }
        session('admin_category_id',$arr['category_id']);
        session('admin_article_sex',$arr['article_sex']);
        $Model=D("Article");
        $result=$Model->add_article_post($arr);
        if($result===false){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }else{
            //echo "<script>alert('提交成功');window.location.href='/article/index/article_add';</script>";
            echo "<script>window.location.href='/article/index/article_add';</script>";
        }
    }

    //删除文章 单个
    public function article_delete()
    {
        $article_id=I('path.2');
        $Model=D("Article");
        $Model->article_id=$article_id;
        if(!$Model->delete_article()){
            echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }else{
            echo "<script>self.location=document.referrer;</script>";exit;
        }
    }

    //删除文章 所选
    public function article_delete_more_post()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            //dump($ids);exit;
            $Model=M("Article");
            foreach($ids as $v){
                if(!$Model->where('article_id='.$v)->delete()){
                    echo "<script>alert('删除失败，请您尝试重新提交');self.location=document.referrer;</script>";exit;
                }
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }
    }


    //博客分类管理
    public function category(){
        //常用设置
        $data['title']='文章分类管理';
        $data['active']='article_category';
        $this->assign("data", $data);
        //分类列表
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        //echo $page;exit;
        $perpage=10;//每页显示
        $start=($page-1)*$perpage;
        //echo $start;exit;
        $CategoryModel=D('ArticleCategory');
        $CategoryModel->page=$page;  //当前页
        $CategoryModel->perpage=$perpage;  //每页显示数
        $list=$CategoryModel->get_category_list();
        //dump($list);exit;
        $this->assign("list", $list);

        $count=M('ArticleCategory')->count();
        $page_total=ceil($count/$perpage);
        //echo $page_total;exit;
        $this->assign("page_total",$page_total);
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $this->assign("pager",$pager);
        //dump($count);exit;
        $this->display();
    }

    //编辑文章分类
    public function category_edit()
    {
        $data['title']='编辑分类';
        $data['active']='article_category';
        $this->assign("data", $data);
        $category_id=I('path.2');
        $Model=M('ArticleCategory');
        $category_info=$Model->where('category_id='.$category_id)->find();
        $parent_category_list=$Model->where('category_parent_id=0 and category_show=1')->field('category_id,category_name')->select();
        //dump($category_info);exit;
        $this->assign("category_info", $category_info);
        $this->assign("parent_category_list", $parent_category_list);
        $this->assign("category_id", $category_id);
        $this->display();
    }

    //编辑文章分类 提交
    public function category_edit_post()
    {
        //提交检测
        if($_POST['category_name']==''){
            echo "<script>alert('请您填写完整后尝试重新提交');history.back(-1);</script>";exit;
        }
        $post_data=I('post.');
        $Model=D("ArticleCategory");
        $result=$Model->where('category_id='.$post_data['category_id'])->save($post_data);
        if($result===false){
            echo "<script>alert('写入数据库失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        //此处写入日志
        echo "<script>alert('提交成功');window.location.href='/article/index/category';</script>";exit;
    }

    //添加文章分类
    public function category_add()
    {
        $data['title']='添加分类';
        $data['active']='article_category';
        $this->assign("data", $data);
        //$category_id=I('path.2');
        $Model=M('ArticleCategory');
        //$category_info=$Model->where('category_id='.$category_id)->find();
        $parent_category_list=$Model->where('category_parent_id=0 and category_show=1')->field('category_id,category_name')->select();
        //dump($parent_category_list);exit;
        //$this->assign("category_info", $category_info);
        $this->assign("parent_category_list", $parent_category_list);
        //$this->assign("category_id", $category_id);
        $this->display();
    }

    //添加文章分类 提交
    public function category_add_post()
    {
        //提交检测
        if($_POST['category_name']==''){
            echo "<script>alert('请您填写完整后尝试重新提交');history.back(-1);</script>";exit;
        }
        $post_data=I('post.');
        $Model=D("ArticleCategory");
        $result=$Model->add($post_data);
        if($result===false){
            echo "<script>alert('写入数据库失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/article/index/category';</script>";exit;
    }

    //删除分类 单个
    public function category_delete()
    {
        $category_id=I('path.2');
        $Model=D("ArticleCategory");
        $result=$Model->where('category_id='.$category_id)->delete();
        if($result===false){
            echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }else{
            echo "<script>self.location=document.referrer;</script>";
        }
    }

    //删除分类 所选
    public function category_delete_more_post()
    {
        //echo 333;exit;
        if(I('post.ids')){
            $ids=I('post.ids');
            //dump($ids);exit;
            $Model=M("articleCategory");
            foreach($ids as $v){
                if(!$Model->where('category_id='.$v)->delete()){
                    echo "<script>alert('删除失败，请您尝试重新提交');self.location=document.referrer;</script>";exit;
                }
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }
    }


    //检查是否登录
    public function check_login(){
        //判断是否登录以及是否管理员帐号，如果不是则跳转到登录页面
        if($_SESSION['is_admin']!=2){
            echo "<script>window.location.href='/login'</script>";exit;
        }
    }

}